import 'package:flutter/material.dart';
import '../model/CommonModel.dart';
import 'dart:convert';

// ignore: must_be_immutable
class CalendarPage extends StatefulWidget {
  String title;

  CalendarPage({this.title});

  @override
  State<StatefulWidget> createState() {
    return CalendarState();
  }

}

class CalendarState extends State<CalendarPage> {
  List<dynamic> list = [];

  @override
  void initState() {
    super.initState();
    loadData();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Color(0xff313131),
        title: Text(
          widget.title,
          style: TextStyle(
            color: Colors.white
          ),
        ),
      ),
      body: PageView.builder(
        scrollDirection: Axis.vertical,
          itemCount: list.length,
          itemBuilder: (context, index) => InkWell(
            child: Padding(
              padding: EdgeInsets.all(15),
              child: Column(
                children: <Widget>[
                  Row(
                    children: <Widget>[
                      Expanded(
                        child: Text((list[index] as Datas).title),
                      ),
                      Image.asset(
                        "image/indiimg_9x17_@2x.png",
                        height: 18,
                        width: 18,
                      )
                    ],
                  ),
                  Expanded(
                      child: Padding(
                        padding: EdgeInsets.symmetric(vertical: 10),
                        child: Image.network((list[index] as Datas).thumbnail),
                      )
                  ),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.end,
                    children: <Widget>[
                      Text("Designed by OWSPACE.")
                    ],
                  )
                ],
              ),
            ),
          )
      )
    );
  }

  void loadData() async {
    String str = await DefaultAssetBundle.of(context).loadString("json/CalendarData.json");
    CommonModel model = CommonModel.fromJson(json.decode(str));
    setState(() {
      list = model.datas;
    });
  }
}